<template>
  <div>
    <el-dialog v-model="formApprove.dialog" title="确认" width="30%" center>
      <template #default>您将{{ formApprove.PopupType }}:{{ formApprove.name }}的申请</template>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="formApprove.dialog = false">取消</el-button>
        <el-button type="danger" @click="returnRow">确定</el-button>
      </span>
      </template>
    </el-dialog>

    <!--搜索框-->
    <div type="flex" style="margin-bottom: 10px;width: 100%;">
      <el-switch
          style="--el-switch-off-color: #13ce66; --el-switch-on-color: #ff4949"
          v-model="approveValue"
          active-text="未通过"
          inactive-text="所有"
          :onchange="getTableData()"
      />
    </div>
    <el-scrollbar>
      <el-table v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
                :row-style="{height: '54.7px'}"
                style="font-size: 15px" stripe :data="tableData">

        <el-table-column label="图片" width="120">
          <template #default="scopeEdit">
            <div class="demo-image">
              <div class="block">
                <el-image style="width: 100px; height: 100px" :src="'/api/common/download?name='+scopeEdit.row.photo"
                          fit="cover"/>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="访问人姓名" width="auto"/>
        <el-table-column label="访问人性别" width="auto">
          <template #default="scope">
            <label> {{ scope.row.sex == 0 ? '女' : '男' }}</label>
          </template>
        </el-table-column>
        <el-table-column prop="phoneNumber" label="访问人电话" width="auto"/>
        <el-table-column prop="idCard" label="身份证号" width="auto"/>
        <el-table-column label="审批情况" width="auto">
          <template #default="scope">
            <label> {{ scope.row.approve == 0 ? '未批准' : '通过' }}</label>
          </template>
        </el-table-column>
        <el-table-column label="删除情况" width="auto">
          <template #default="scope">
            <label> {{ scope.row.isDelete == 0 ? '未删除' : '已删除' }}</label>
          </template>
        </el-table-column>
        <el-table-column prop="accessTime" label="访问时间" width="auto"/>

        <el-table-column fixed="right" label="操作" v-if="!approveValue">
          <template #default="scopeEdit">
            <el-button
                v-show="scopeEdit.row.approve === 0"
                link
                type="success"
                size="small"
                @click.prevent="approveInitialize(scopeEdit.row)">
              <el-icon>
                <Check/>
              </el-icon>
              <span>同意</span>
            </el-button>
            <el-button
                v-show="(scopeEdit.row.approve === 1||scopeEdit.row.approve === 0)"
                link
                type="danger"
                size="small"
                @click.prevent="cancelInitialize(scopeEdit.row )">
              <el-icon>
                <Remove/>
              </el-icon>
              <span>不同意</span>
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-scrollbar>

  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref, watch} from "vue";
import {Check, Delete, Edit, Operation, Search} from '@element-plus/icons-vue'

import {ElMessage} from "element-plus";
import {ElLoading} from "element-plus/es";
import {approveApi, cancelApi, getAllApi, getRemovedApi} from "@/api/Access";


//数据初始化
onMounted(() => {
  getTableData()
})

const approveValue = ref(false)

//分页
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
})


//分页操作
const tableData = ref([])
const loading = ref(true)
const search = reactive({
  author: "",
  bookName: "",
  tid: ""
})

function getTableData() {
  console.log(approveValue.value)
  if (approveValue.value) {
    getRemovedApi().then((response: any) => {
      if (response.code === 200) {
        tableData.value = response.data
      } else {
        ElMessage.warning(response.msg)
      }
      loading.value = false
    }).catch(function (res) {
      loading.value = false
      ElMessage.error("页面加载失败")
    })
  } else {
    getAllApi().then((response: any) => {
      if (response.code === 200) {
        tableData.value = response.data
      } else {
        ElMessage.warning(response.msg)
      }
      loading.value = false
    }).catch(function (res) {
      loading.value = false
      ElMessage.error("页面加载失败")
    })
  }

  return true

}


//添加修改数据初始化
const BtnConfirmAble = ref(true)


const formEdit = reactive({
  author: "",
  bid: "",
  bookName: "",
  num: 0,
  press: "",
  tid: "",
  PopupType: ""
})


//修改字段检测
watch(() => ({...formEdit}), (count, prevCount) => {
  BtnConfirmAble.value = count.author == "" ||
      count.bookName == "" ||
      count.num == 0 ||
      count.press == "" ||
      count.tid == "";
})


function Cancel() {
  editDialogVisible.value = false;
}


//添加系统用户
const editDialogVisible = ref(false)

const PopupType = ref(['通过', '撤销'])


const formApprove = reactive({
  id: -1,
  name: '',
  PopupType: '',
  dialog: false
})


function approveInitialize(row: any) {
  formApprove.id = row.id
  formApprove.name = row.name
  formApprove.dialog = true;
  formApprove.PopupType = PopupType.value[0];
}

function cancelInitialize(row: any) {
  formApprove.id = row.id
  formApprove.name = row.name
  formApprove.dialog = true;
  formApprove.PopupType = PopupType.value[1];
}

function returnRow() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  if (formApprove.PopupType == PopupType.value[0]) {
    approveApi(formApprove.id)
        .then(function (response: any) {
          if (response.code == 200) {
            formApprove.dialog = false
            getTableData()
            ElMessage.success("操作成功")
          }
          loading.close()
        }).catch(response => {
      ElMessage.error("操作失败")
      loading.close()
    })
  } else {
    cancelApi(formApprove.id)
        .then(function (response: any) {
          if (response.code == 200) {
            formApprove.dialog = false
            getTableData()
            ElMessage.success("操作成功")
          }
          loading.close()
        }).catch(response => {
      ElMessage.error("操作失败")
      loading.close()
    })
  }

}

const formLabelWidth = '140px'
const inputSize = 'default'

</script>
